<%@page isELIgnored="false" pageEncoding="UTF-8" contentType="text/html; UTF-8" %>

<script>
    $(function () {
        $("#articlelist").datagrid({
            url:"${pageContext.request.contextPath}/lucene/queryByKeyword",
            pagination:true,
            toolbar:"#tb",
            columns:[[

                {field:'articleId',title:'ID',width:100},
                /*{field:'articleImage',align:"IMAGE",title:'图片',width:100,align:'right',
                    formatter: function(value,row,index){

                        return "<image  height='30px' src='${pageContext.request.contextPath}"+row.articleImage+"'>";
                    }},*/
                {field:'articleName',title:'OLDNAME',width:250,align:'right'},
                {field:'articleContent',title:'STATE',width:400,align:'right'},
                {field:'guruId',title:'DATE',width:100,align:'right'},
                {field:'articleDate',title:'DESCRIPTION',width:200,align:'right'},
            ]],
            onDblClickRow:function (rowIndex,rowData) {
                openUpdate(rowData);
            }
        });
        $('#adddialog').dialog({
            title: '添加文章',
            width: 400,
            height: 200,
            closed: true,
            buttons:"#addbtn",

        });
        $('#updatedialog').dialog({
            title: '添加文章',
            width: 400,
            height: 200,
            closed: true,
            buttons:"#updatebtn",

        });

    })

    function openaddDialog(){
        $('#adddialog').dialog("open");
        $.ajax({
            url:"${pageContext.request.contextPath}/getAll",
            type:"post",
            dataType:"json",
            success:function (data) {
                for(var i=0;i<data.length;i++){
                    $("#guruname").append("<option value='"+data[i].guruId+"'>"+data[i].guruName+"</option>");
                }
            }
        })
    }

    function doadd(){
        $('#addform').form('submit', {
            url:"${pageContext.request.contextPath}/add",
            onSubmit: function(){
                // do some check
                // return false to prevent submit;
                return true;
            },
            success:function(data){
                var jsobj=JSON.parse(data);
                $.messager.alert('我的消息',jsobj.message,'info');
                $("#articlelist").datagrid("load");
                $('#adddialog').dialog("close");
            }
        });

    }

    function canceladd(){
        $('#adddialog').dialog("close");
    }



    function openUpdate(rowData){
        $("#updateguruname").html("");
        $('#updatedialog').dialog("open");
        $("#articleName").val(rowData.articleName);
        $("#articleContent").val(rowData.articleContent);
        $("#updateId").val(rowData.articleId);
        $.ajax({
            url:"${pageContext.request.contextPath}/getAll",
            type:"post",
            dataType:"json",
            success:function (data) {
                for(var i=0;i<data.length;i++){
                    if(data[i].guruId==rowData.guruId){
                        $("#updateguruname").append("<option selected='selected' value='"+data[i].guruId+"'>"+data[i].guruName+"</option>");

                    }else{
                        $("#updateguruname").append("<option value='"+data[i].guruId+"'>"+data[i].guruName+"</option>");

                    }
                }
            }
        })
    }


    function doupdate(){
        $('#updateform').form('submit', {
            url:"${pageContext.request.contextPath}/update",
            onSubmit: function(){
                // do some check
                // return false to prevent submit;
                return true;
            },
            success:function(data){
                var jsobj=JSON.parse(data);
                $.messager.alert('我的消息',jsobj.message,'info');
                $("#articlelist").datagrid("load");
                $('#adddialog').dialog("close");
            }
        });
    }

    function searchkenyword(){
        var keyword=$("#keyword").val();
        $("#articlelist").datagrid("load",{
            "keyword":keyword
        });
    }


    function updateIndexDB(){
        $.ajax({
            url:"${pageContext.request.contextPath}/lucene/resetIndexDB",
            dataType:"json",
            success:function (data) {
                $.messager.alert('我的消息',data.message,'info');
            }

        })
    }

</script>


<table id="articlelist"></table>

<div id="tb">
    <a href="javascript:openaddDialog()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">添加</a>
    <input id="keyword">
    <a href="javascript:searchkenyword()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">检索文章</a>
    <a href="javascript:updateIndexDB()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">更新检索</a>

</div>

<div id="adddialog">
    <form id="addform" method="post" >
        文章标题：<input name="articleName">    <br>
        文章内容：<textarea name="articleContent"></textarea><br>
        所属大师：<select id="guruname" name="guruId">

                </select>
    </form>

</div>
<div id="addbtn">
    <a href="javascript:doadd()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">确认</a>
    <a href="javascript:canceladd()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">取消</a>
</div>
<div id="updatedialog">
    <form id="updateform" method="post">
        <input type="hidden" id="updateId" name="articleId">
        文章标题：<input name="articleName" id="articleName">    <br>
        文章内容：<textarea name="articleContent" id="articleContent"></textarea><br>
        所属大师：<select id="updateguruname" name="guruId">

                </select>

    </form>
</div>
<div id="updatebtn">
    <a href="javascript:doupdate()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">确认</a>
    <a href="javascript:cancelupdate()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">取消</a>
</div>